// Linux filesystem attribute support
// Copyright 2005 (C) Ralph Thomas

#include <model/file/linuxAttr.h>
#include <sys/types.h>
#include <attr/xattr.h>
#include <model/error.h>
#include <vector>

#include <iostream>

namespace bfs = boost::filesystem;


namespace model {
	int setAttr( const bfs::path& target, const std::string& name,
		const std::string& value ) {
		std::string attrName( "user." + name );
		std::string path( target.string() );
		//
		// If this call returns zero then all went well.
		//
		if( !setxattr( path.c_str(), attrName.c_str(), value.c_str(), value.size(), 0 ) )
			return kErrorNone;
		//
		// Something bad happened; for now assume that we can't write attributes.
		//
		return kErrorReadOnly;
	}
	int getAttr( const bfs::path& target, const std::string& name, std::string& value ) {
		std::string attrName( "user." + name );
		std::string path( target.string() );
		std::vector<char> buffer;
		//
		// First we need to find out how large this attribute actually is.
		//
		ssize_t size( getxattr( path.c_str(), attrName.c_str(), 0, 0 ) );
		if( size < 1 ) return kErrorNonExistant;
		//
		// Make a buffer big enough for the value, and then fetch it.
		//
		buffer.resize( size );
		getxattr( path.c_str(), attrName.c_str(), &(buffer[0]), size );
		//
		// Set the out value and return success.
		//
		value = std::string( &(buffer[0]), size );
		return kErrorNone;
	}
};

